import * as types from '../types'
import axios from 'axios'

const state = {
  lives: [],
  total: 0
}

const getters = {
  lives: state => state.lives,
  totalLives: state => state.total
}

const mutations = {
  [types.LIVES_LOADED](state, data) {
    state.total = data.total || 0
    state.lives = data.items || []
  }
}

const actions = {
  async getLives({
    commit
  }, data) {
    const response = await axios.post('/vts/lives/search', data.query)
    data.callback(response.data)
  },
  async searchlives({
    commit
  }, query) {
    let response = await axios.post('/vts/lives/search', query)
    commit(types.LIVES_LOADED, response.data)
  },
  async saveLive({
    commit
  }, data) {
    let response = await axios.post('/vts/lives/save', data.form)
    if (response.data.saved) data.callback()
  },
  async removeLive({
    commit
  }, data) {
    let response = await axios.post('/vts/lives/remove', {
      id: data.id
    })
    if (response.data.removed) data.callback()
  },
  async getLive({
    commit
  }, data) {
    let response = await axios.get('/vts/lives/detail?id=' + data.id)
    data.callback(response.data)
  }
}

export default {
  state,
  mutations,
  getters,
  actions
}
